package itqf.service;



import itqf.dao.OrdersDaoImpl;
import itqf.dao.impl.OrdersDao;
import itqf.entity.Cart;
import itqf.entity.Item;
import itqf.entity.Orders;
import itqf.service.impl.CartService;
import itqf.service.impl.ItemService;
import itqf.service.impl.OrdersService;
import itqf.utils.OrdersUtils;

import java.util.Date;
import java.util.List;

/**
 * com.itqf.service
 * 2020/9/17
 */
public class OrdersServiceImpl implements OrdersService {
    private OrdersDao ordersDao = new OrdersDaoImpl();
    private CartService cartService = new CartServiceImpl();
    private ItemService itemService  =new ItemServiceImpl();
    @Override
    public int addOrder(Orders orders) {
        //添加订单信息
            //生成随机的订单ip
        String oid = OrdersUtils.createOrderId();
            //生成当下的订单时间
        Date date = OrdersUtils.DateFormat();
        orders.setOid(oid);
        orders.setOtime(date);
        orders.setOstate(0);

        int i= ordersDao.addOrder(orders);
        //添加订单详情
        List<Cart> list = cartService.findAllCart(orders.getUid());
        for (Cart cart : list) {
            Item item = new Item();
            item.setOid(oid);
            item.setPid(cart.getPid());
            item.setIcount(cart.getCcount());
            item.setInum(cart.getCnum());
            i+=itemService.addItem(item);
        }

        //3.清空用户的购物车
        i+=cartService.delCart(orders.getUid(),0);
        return i;
    }

    @Override
    public List<Orders> findAllOrderByUid(int uid) {
        return ordersDao.findAllOrderByUid(uid);
    }

    @Override
    public int updateOrderState(Orders orders){
        return ordersDao.updateOrderState(orders);
    }

}
